include("shared.lua")

function ENT:Initialize()
	self:DrawShadow(false)
	self:SetRenderBounds(Vector(-100, -100, -100), Vector(100, 100, 100))

	local car = self:GetOwner()
	if car:IsValid() then
		car:AddPowerup(self)
	end

	self.AmbientSound = CreateSound(self, "Missile.Ignite")
	self.Emitter = ParticleEmitter(self:GetPos())
	self.Emitter:SetNearClip(16, 22)
	self.PixVis = util.GetPixelVisibleHandle()
end

function ENT:Think()
	self.Emitter:SetPos(self:GetPos())

	local vehicle = self:GetOwner()
	if 0 < self:GetSkin() and vehicle:IsValid() then
		self.AmbientSound:PlayEx(0.98, 255 - math.min(180, vehicle:GetVelocity():Length() * 0.035 * RSCALE))
	end
end

function ENT:OnRemove()
	self.Emitter:Finish()
	self.AmbientSound:Stop()

	local car = self:GetOwner()
	if car:IsValid() then
		car:RemovePowerup(self)
	end
end

function ENT:DrawDashboard(pos, ang, scale, i)
	local y = i * 50

	surface.SetDrawColor(0, 0, 0, 255)
	surface.DrawRect(400, y, 200, 32)
	surface.SetDrawColor(30, 30, 255, 255)
	surface.DrawRect(400, y, (100 - self:GetSkin()) * 0.01 * 200, 32)
	surface.SetDrawColor(255, 255, 255, 255)
	surface.DrawOutlinedRect(400, y, 200, 32)

	draw.SimpleText("MEGA BOOSTER", "ScoreboardSub", 500, y + 16, COLOR_RED, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
end

local colFlame = Color(255, 150, 30, 255)
local colLight = Color(255, 255, 255, 255)
local matLight = Material("sprites/light_ignorez")
function ENT:DrawHeadlight(pos, normal)
	local ViewNormal = pos - EyePos()
	local Distance = ViewNormal:Length() * SCALE
	ViewNormal:Normalize()
	local ViewDot = ViewNormal:Dot(normal) * -1

	if ViewDot >= 0 then
		local Visibile = util.PixelVisible(pos, 16 * SCALE, self.PixVis)
		if not Visibile then return end

		render.SetMaterial(matLight)
		local Size = math.Clamp(Distance * Visibile * 3 * ViewDot, 128 * SCALE, 700 * SCALE)
		Distance = math.Clamp(Distance, 32 * SCALE, 800 * SCALE)
		colFlame.a = math.Clamp((1000 * SCALE - Distance) * Visibile * 1.5 * ViewDot, 0, 240)
		render.DrawSprite(pos, Size, Size, colFlame, Visibile * ViewDot)
		colLight.a = colFlame.a
		render.DrawSprite(pos, Size * 0.4, Size * 0.4, colLight, Visibile * ViewDot)
	end
end

function ENT:DrawTranslucent()
	local owner = self:GetOwner()
	if not owner:IsValid() then return end

	self:SetColor(owner:GetColor())
	self:SetModelScale(VSCALE)
	self:DrawModel()

	render.SetMaterial(matLight)

	if 0 < self:GetSkin() then
		local dir = self:GetUp()
		local vPos = self:GetPos() + SCALE * 64 * dir

		self:DrawHeadlight(vPos, dir)

		local particle
		if util.PointContents(vPos) and CONTENTS_WATER == 32 then
			particle = self.Emitter:Add("effects/bubble", vPos)
		else
			particle = self.Emitter:Add("effects/fire_cloud1", vPos)
		end
		particle:SetVelocity(100 * SCALE * dir + math.Rand(16, 32) * SCALE * VectorRand())
		particle:SetStartAlpha(160)
		particle:SetEndAlpha(0)
		particle:SetStartSize(math.Rand(24, 32) * SCALE)
		particle:SetEndSize(0)
		particle:SetDieTime(math.Rand(0.5, 0.7))
		particle:SetAirResistance(2)
		particle:SetRoll(math.Rand(0, 360))
		particle:SetRollDelta(math.Rand(-4, 4))
	end
end
